<?php 
	$exchangeId = $this->getRequest()->getParam('exchange_id'); 
    $templateId = $this->getRequest()->getParam('template_id'); 
    $imgurl = $this->getSkinUrl('/images/ajax-loader-tr.gif');
 ?>
<form id="submit_form" method="post" action="<?php echo $this->getUrl('admin_promotion/adminhtml_exchange/saveTemplet'); ?>" enctype="multipart/form-data">
<div>
<input name="form_key" type="hidden" value="<?php echo Mage::getSingleton('core/session')->getFormKey() ?>" />
	<input name="exchangeId" id="exchangeId" type="hidden" value="<?php echo $exchangeId; ?>" />
</div>
<div id="centerimage"><img src="<?php echo $imgurl ?>"/></div>
<div class="content-header">
    <table cellspacing="0">
        <tbody><tr>
            <td class="a-right">
                    <span id="coupons_generate_button"><button id="id_3cf81872242ced1ce5317b5704e032c7" title="保存" type="button" class="scalable generate" onclick="resetTemplate()"><span><span><span>重置模板</span></span></span></button></span>
                    <span id="coupons_generate_button"><button id="id_3cf81872242ced1ce5317b5704e032c7" title="保存" type="button" class="scalable generate" onclick="saveTemplet()"><span><span><span>保存模板</span></span></span></button></span>
            </td>
        </tr>
    </tbody></table>
</div>
<div class="entry-edit">
    <div class="entry-edit-head">
    <h4 class="icon-head head-edit-form fieldset-legend">模板操作</h4>
</div>
<div class="fieldset  ignore-validate" id="coupons_information_fieldset">
    <div class="hor-scroll">
        <table cellspacing="0" class="form-list">
            <tbody>
    			<tr>
    			    <td class="label"><label for="templet">选择模板</label></td>
    			    <td class="value">
                        <?php if($templateId):?>
                            <select id="templet" name="templet" class="required-entry select">
                            <option value="<?php echo $templateId; ?>" selected="selected">
                            <?php
                                 if($templateId==1){
                                    echo "积分APP版";
                                 }else{
                                     echo "积分PC版";
                                 }
                              ?></option>
                            </select>
                        <?php else: ?>
    			    	<select id="templet" name="templet" class="required-entry select">
        			    	<option value="0">请选择合适的模板</option>
        			    	<option value="1">积分APP版</option>
        			    	<option value="2">积分PC版</option>
    			    	</select>
                    <?php endif; ?>
    			    </td>  
        		</tr>
            </tbody>
        </table>
    </div>
</div>
<div id ="contents">
		
</div>

</form>
<script type="text/javascript">
    var  $j = jQuery.noConflict();  
    $j(document).ready(function(){

        var value=$j("#templet").children('option:selected').val();
        if(value!=0){
            $j("#centerimage").show();
            loadTemplate(value); 
        }



        $j('#templet').change(function(){ 
            var value=$j(this).children('option:selected').val();
                if(value==0){
                    layer.alert("请选择模板");
                    return false;
                }
                $j("#centerimage").show();
        	    loadTemplate(value);

        });

    })

    function loadTemplate(value){
            var url ="<?php echo $this->getUrl('admin_promotion/adminhtml_exchange/loadTemplet'); ?>";
            var exchangeId = $j("#exchangeId").val();
            var url = url + "value/"+value+"/exchange_id/"+exchangeId;
            $j.get(url,function(result){   
                if(result.error){
                    $j("#centerimage").hide();
                    alert(result.message);
                }else{
                    $j("#centerimage").hide();
                    $j("#contents").html(result.message);
                }
             },"json");
    }

	function saveTemplet(){
        var value=$j("#templet").children('option:selected').val();
        if(value==0){
            layer.alert("请选择模板");
            return false;
        }
    	$j("#submit_form").submit();
    }

    function resetTemplate(){
        layer.confirm("确定要重置模板为初始状态？",function(index){
            var exchangeId = "<?php echo $exchangeId; ?>";
            var template_id = "<?php echo $templateId ; ?>";
            var url ="<?php echo $this->getUrl('admin_promotion/adminhtml_exchange/resettemplate'); ?>";
            var url = url + "exchange_id/"+exchangeId+"/template_id/"+template_id;
              $j("#centerimage").show();
            $j.get(url,function(result){
                if(result.error){
                      $j("#centerimage").hide();
                    var urls = "<?php echo $this->getUrl('admin_promotion/adminhtml_exchange/edit'); ?>";
                    if(template_id==""){
                        template_id = 0;
                    }
                    var urls = urls+"exchange_id/"+exchangeId+"/template_id/"+template_id;
                    window.location.href=urls;    
                }else{
                    layer.alert(result.message);
                }
             },"json");
            layer.close(index);
         });
    }




</script>